<?php
require_once 'dbi.php';
require_once 'form_builder.php';

class general
{
	function general()
	{
		$this->dbconn();
	}
	
	function dbconn()
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		$db = mysql_select_db(DBNAME, $conn);
		return $conn;
	}
	
	function executeMysql($sql)
	{
		$result = mysql_query($sql);
		
		return $result;
	}
	
	function form_register()
	{
		$form = new form_builder();
		
		$form->newForm("register", "register.php","width:500px");
				
		$form->addElement("text", "", "name","Name :");
		$form->addElement("password", "", "password","Password :");
		$form->addElement("password", "", "confirm_password","Confirm Password :");
		$form->addElement("submit", "Register", "submit");
		
		$form->closeForm();
		$form->formShow();
		
		if ($form->validate())
		{
			$sql = "INSERT INTO user 
					(name,password)
					VALUES
					('".$_REQUEST['name']."','".$_REQUEST['password']."')";
			$result = $this->executeMysql($sql);
						
			$_SESSION['auth'] = 1;
			$_SESSION['username'] = $_REQUEST['name'];
			echo "<script>location='index.php'</script>"; 
		}
	}
	
	function form_login()
	{
		$form = new form_builder();
		
		$form->newForm("login", "login.php","width:500px;");
		
		$form->addElement("text", "", "name","Username :");
		$form->addElement("password", "", "password","Password :");
		$form->addElement("submit", "Login", "submit");
		
		$form->closeForm();
		$form->formShow();
		$retval = "";
		if ($form->validate())
		{
			$sql = "SELECT * FROM user WHERE name = '".$_REQUEST['name']."' AND password = '".$_REQUEST['password']."'";
			$result = $this->executeMysql($sql);
			
			$num = mysql_num_rows($result);
			
			if ($num > 0)
			{
				$_SESSION['auth'] = 1;
				$_SESSION['username'] = $_REQUEST['name'];
				echo "<script>location='index.php'</script>";
				
			}
			else
			{
				$retval = "<p style='color:red'>Invalid Login</p>";
			}
		}
		echo $retval;
	}
	
	function form_list_dj()
	{
		$sql = "SELECT * FROM user";
		$result = $this->executeMysql($sql);
		
		
	}
	
	function showUserInfo()
	{
		$retval = "";
		if (isset($_SESSION['auth']))
		{
			$retval = "<p>Welcome Back <span style='color:white'>".$_SESSION['username']."</span></p>";
		}
		return $retval;
	}
}

?>